package com.victualler.core.common.utils;


import com.victualler.core.common.enums.ChecKDataErrorEnum;
import com.victualler.core.common.exception.CoreRuntimeException;

/**
 * @Aouthor Guoshichang
 * @Date 11:45 on 2018/6/7.
 * @Function 避免空指针异常等导致catch不到对应的异常信息
 */
public class ExceptionWapperUtil {
    /**
     * @Aouthor Guoshichang
     * @Date 11:47 on 2018/6/7.
     * @param  throwable
     * @Function  将异常包装为 runtimeException
     */
    public static CoreRuntimeException wapper(Throwable throwable){
        if(throwable instanceof CoreRuntimeException){
            if(throwable.getCause()!= null && throwable.getCause() instanceof CoreRuntimeException){
                throwable = throwable.getCause();
            }
            return (CoreRuntimeException)throwable;
        }else{
            throwable.printStackTrace();
            return ChecKDataErrorEnum.createException(ChecKDataErrorEnum.RUN_ERROR,throwable.getLocalizedMessage());
        }
    }
}
